package com.naiterui.ehp.bs.patient.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.naiterui.ehp.bp.domain.DrPhoneContact;
import com.naiterui.ehp.bs.patient.dao.IDrPhoneContactDao;
import com.naiterui.ehp.bs.patient.repository.IApDrPhoneContactRepository;

/**
 * 医生手机联系人持久层实现
 *
 * @author Amber
 * @since 1.0.0
 */
@Component
public class ApDrPhoneContactDaoImpl implements IDrPhoneContactDao {

    @Autowired
    private IApDrPhoneContactRepository drPhoneContactRepository;

    @Override
    public void saveOrUpdate(DrPhoneContact contact) {
        drPhoneContactRepository.save(contact);
    }

    @Override
    public Map<Long, DrPhoneContact> getContactMapByPhone(String phone) {
        // 获取手机号相关手机联系人记录（未绑定患者的），
        List<DrPhoneContact> contacts = drPhoneContactRepository.findByPhoneAndPatientIdIsNull(phone);
        // 处理相关结果集为map key:医生id，value：手机联系人
        Map<Long, DrPhoneContact> resultMap = new HashMap<>();
        for (DrPhoneContact contact : contacts) {
            resultMap.put(contact.getDoctorId(), contact);
        }
        return resultMap;
    }

    @Override
    public DrPhoneContact getUnBindContactByDr(String phone, Long doctorId) {
        List<DrPhoneContact> contacts = drPhoneContactRepository.findByPhoneAndDoctorIdAndPatientIdIsNull(phone, doctorId);
        return contacts.size() > 0 ? contacts.get(0) : null;
    }

}
